package controller;


import entity.Make;
import entity.Patient;
import entity.Schedule;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.MakeService;

import javax.annotation.Resource;
import java.beans.PropertyEditorSupport;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/make")
@Scope("prototype")
public class MakeController {
    @Resource(name = "makeService")
    MakeService makeService;


/*    @Resource(name = "redisService")
    RedisService redisService;*/


    @RequestMapping("/query")
    @ResponseBody
    Map<String ,Object> GetAll(@RequestParam(value = "pageNumber",defaultValue = "1",required = false) int pageNumber, @RequestParam(value = "pageSize",defaultValue = "5",required = false) int pageSize, String pName, Make make){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("cLastDate",make.getcLastDate());
        map.put("cFirstDate",make.getcFirstDate());
        map.put("pName",pName);
        map.put("offset",(pageNumber-1)*pageSize);
        map.put("limit",pageSize);
        return makeService.GetMakeInfo(map);
    }

    @RequestMapping("/GetscheduleInfo")
    @ResponseBody
    Map<String,Object> GetscheduleInfo(int offset,int limit){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("limit",limit);
        map.put("offset",offset);
       return makeService.GetSchedule(map);
    }


    @RequestMapping("/DataView")
    @ResponseBody
    List<Schedule> GetGuaDataView(Integer Pid){
        return makeService.GetMakeByDoctorId(Pid) ;
    }


    @RequestMapping("/GetUserInfoByGuaId")
    @ResponseBody
    Patient GetUserInfoByGuaId(Integer id){
        if (id!=0){
            return makeService.GetUserInfoByid(id);
        }
        return null;
    }


    /**
     * 添加日程
     */
    @RequestMapping("/addSchedule")
    @ResponseBody
    boolean AddMake(Schedule schedule,String id,int pId){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("id",id);
        map.put("schedule",schedule);
        map.put("pId",pId);
        return  makeService.AddMake(map);
    }




    /**
     * 允许传入空日期
     * @param dataBinder
     */
    @InitBinder
    public void InitBinder(WebDataBinder dataBinder)
    {
        dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            public void setAsText(String value) {
                try {
                    setValue(new SimpleDateFormat("yyyy-MM-dd").parse(value));
                } catch(ParseException e) {
                    setValue(null);
                }
            }
            public String getAsText() {
                return new SimpleDateFormat("yyyy-MM-dd").format((Date) getValue());
            }
        });
    }
}
